﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using model;
using System.Data;
using System.Data.SqlClient;

namespace dao
{
    public class LoaisanphamDao:BaseDao
    {
        public List<LoaiSanphamTrathat> getListLoaiSanphamByNhom(long id)
        {
            sqlCmd.CommandType = CommandType.StoredProcedure;
            sqlCmd.CommandText = "sp_loaisanpham_ByNhom";
            sqlCmd.Parameters.Clear();
            sqlCmd.Parameters.Add("@IDNhomsanpham", SqlDbType.BigInt).Value = id;
            sqlCmd.Connection.Close();
            sqlCmd.Connection.Open();

            using (SqlDataReader dr = sqlCmd.ExecuteReader())
            {
                List<LoaiSanphamTrathat> glstLoaiSanpham = new List<LoaiSanphamTrathat>();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        LoaiSanphamTrathat sp = new LoaiSanphamTrathat();
                        sp.ID = Convert.ToInt64(dr["ID"]);
                        sp.Tenloaisanpham = Convert.ToString(dr["Tenloai"]);
                        sp.IDNhomsanpham = Convert.ToInt64(dr["IDNhomsanpham"]);
                        
                        glstLoaiSanpham.Add(sp);
                    }
                    dr.Close();
                    sqlCmd.Connection.Close();
                }
                return glstLoaiSanpham;
            }
        }

        public List<LoaiSanphamTrathat> getListLoaiSanphamByID(long id)
        {
            sqlCmd.CommandType = CommandType.StoredProcedure;
            sqlCmd.CommandText = "sp_loaisanpham_SelectRow";
            sqlCmd.Parameters.Clear();
            sqlCmd.Parameters.Add("@ID", SqlDbType.BigInt).Value = id;
            sqlCmd.Connection.Close();
            sqlCmd.Connection.Open();

            using (SqlDataReader dr = sqlCmd.ExecuteReader())
            {
                List<LoaiSanphamTrathat> glstLoaiSanpham = new List<LoaiSanphamTrathat>();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        LoaiSanphamTrathat sp = new LoaiSanphamTrathat();
                        sp.ID = Convert.ToInt64(dr["ID"]);
                        sp.Tenloaisanpham = Convert.ToString(dr["Tenloai"]);
                        sp.IDNhomsanpham = Convert.ToInt64(dr["IDNhomsanpham"]);

                        glstLoaiSanpham.Add(sp);
                    }
                    dr.Close();
                    sqlCmd.Connection.Close();
                }
                return glstLoaiSanpham;
            }
        }
    }
}
